package com.example.idachuappone.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore.Images.Media;
import android.util.DisplayMetrics;
import android.widget.ImageView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;

public class Utils {
    Context context;

    public class LoadBitAsynk extends AsyncTask<String, Integer, Bitmap> {
        ImgCallBack icb;
        ImageView imageView;

        LoadBitAsynk(ImageView imageView, ImgCallBack icb) {
            this.imageView = imageView;
            this.icb = icb;
        }

        protected Bitmap doInBackground(String... params) {
            Bitmap bitmap = null;
            try {
                if (params != null) {
                    for (String str : params) {
                        bitmap = Utils.this.getPathBitmap(Uri.fromFile(new File(str)), PullToRefreshBase.SMOOTH_SCROLL_DURATION_MS, PullToRefreshBase.SMOOTH_SCROLL_DURATION_MS);
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return bitmap;
        }

        protected void onPostExecute(Bitmap result) {
            super.onPostExecute(result);
            if (result != null) {
                this.icb.resultImgCall(this.imageView, result);
            }
        }
    }

    public Utils(Context context) {
        this.context = context;
    }

    public static int getScreenWidth(Activity activity) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return metrics.widthPixels;
    }

    public static int getScreenHeight(Activity activity) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return metrics.heightPixels;
    }

    public static int dip2px(Context context, float dpValue) {
        return (int) ((dpValue * context.getResources().getDisplayMetrics().density) + 0.5f);
    }

    public static int px2dip(Context context, float pxValue) {
        return (int) ((pxValue / context.getResources().getDisplayMetrics().density) + 0.5f);
    }

    public ArrayList<String> listAlldir() {
        Uri uri = new Intent("android.intent.action.PICK", Media.EXTERNAL_CONTENT_URI).getData();
        ArrayList<String> list = new ArrayList();
        Cursor cursor = this.context.getContentResolver().query(uri, new String[]{"_data"}, null, null, null);
        while (cursor.moveToNext()) {
            list.add(new File(cursor.getString(0)).getAbsolutePath());
        }
        return list;
    }

    public Bitmap getPathBitmap(Uri imageFilePath, int dw, int dh) throws FileNotFoundException {
        Options op = new Options();
        op.inJustDecodeBounds = true;
        Bitmap decodeStream = BitmapFactory.decodeStream(this.context.getContentResolver().openInputStream(imageFilePath), null, op);
        int wRatio = (int) Math.ceil((double) (((float) op.outWidth) / ((float) dw)));
        int hRatio = (int) Math.ceil((double) (((float) op.outHeight) / ((float) dh)));
        if (wRatio > 1 && hRatio > 1) {
            if (wRatio > hRatio) {
                op.inSampleSize = wRatio;
            } else {
                op.inSampleSize = hRatio;
            }
        }
        op.inJustDecodeBounds = false;
        return BitmapFactory.decodeStream(this.context.getContentResolver().openInputStream(imageFilePath), null, op);
    }

    public String getfileinfo(String data) {
        String[] filename = data.split("/");
        return filename != null ? filename[filename.length - 2] : null;
    }

    public void imgExcute(ImageView imageView, ImgCallBack icb, String... params) {
        new LoadBitAsynk(imageView, icb).execute(params);
    }
}
